package com.pro.app.controller;


import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pro.core.util.ResponseUtil;
import com.pro.core.validator.NotNullValidator;
import com.pro.db.domain.BbsForum;
import com.pro.db.service.itf.IBbsForumService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.baidu.unbiz.fluentvalidator.ResultCollectors.toComplex;


/**
 * <p>
 * 板块表 前端控制器
 * </p>
 *
 * @author 王雷
 * @since 2020-01-07
 */
@RestController
@RequestMapping("/app/bbsForum")
public class AppBbsForumController {
    private final Log logger = LogFactory.getLog(AppBbsForumController.class);

    @Autowired
    private IBbsForumService bbsForumService;

    /**
     * @apiDefine bbsForum 板块接口
     *
     */

    /**
     * @api {GET} /app/bbsForum/list 4.1 获取板块列表
     * @apiName bbsForum list
     * @apiGroup bbsForum
     * @apiVersion 1.0.0
     * @apiDescription 接口负责人-王雷
     *
     * @apiParam {int} siteId 站点id
     * @apiParam {int} shortLink 短链接
     * @apiParam {int} [page] 页码
     * @apiParam {int} [size] 每页数量
     *
     * @apiSuccess {int} errno 结果码 0:正常返回,-1服务异常
     * @apiSuccess {String} errmsg 前端提示消息
     * @apiSuccess {Object} data 结果集
     * @apiSuccessExample Success-Response:
     * HTTP/1.1 200 OK
    {
     *     "errno": 0,
     *     "errmsg": "成功"
     * }
     */
    @GetMapping("/list")
    public Object list(@NotNull Integer siteId,@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "20") Integer size) {
        Page<BbsForum> pageVo = new Page<>();
        pageVo.setCurrent(page);
        pageVo.setSize(size);
        IPage<BbsForum> pList = bbsForumService.page(pageVo,
                new QueryWrapper<BbsForum>()
                        .eq("deleted",0)
                        .eq("site_id",siteId)
                        .eq("pid",0)
                        .orderByDesc("add_time"));

        for(BbsForum forum:pList.getRecords()){
             List<BbsForum> subs = bbsForumService
                     .list(new QueryWrapper<BbsForum>()
                     .eq("pid",forum.getId())
                     .eq("deleted",0));
             forum.setSubForumList(subs);
        }

        Map<String, Object> data = new HashMap<>();
        data.put("total", pList.getTotal());
        data.put("items", pList.getRecords());
        return ResponseUtil.ok(data);
    }


    /**
     * @api {GET} /app/bbsForum/read 4.2 板块详情
     * @apiName bbsForum read
     * @apiGroup bbsForum
     * @apiVersion 1.0.0
     * @apiDescription 接口负责人-王雷
     *
     * @apiParam {int} id 板块id
     *
     * @apiSuccess {int} errno 结果码 0:正常返回,-1服务异常
     * @apiSuccess {String} errmsg 前端提示消息
     * @apiSuccess {Object} data 结果集
     * @apiSuccessExample Success-Response:
     * HTTP/1.1 200 OK
    {
     *     "errno": 0,
     *     "errmsg": "成功"
     * }
     */
    @GetMapping("/read")
    public Object read(@NotNull Integer id) {
        BbsForum entity = bbsForumService.getById(id);
        entity.setTodayTopicNum(123132);
        entity.setRankNum(1);
        return ResponseUtil.ok(entity);
    }

    /**
     * @api {GET} /app/bbsForum/hot 4.3 获取站点下的热门板块
     * @apiName bbsForum hot
     * @apiGroup bbsForum
     * @apiVersion 1.0.0
     * @apiDescription 接口负责人-王雷
     *
     * @apiParam {int} siteId 站点id
     * @apiParam {int} [page] 页码
     * @apiParam {int} [size] 每页数量
     *
     * @apiSuccess {int} errno 结果码 0:正常返回,-1服务异常
     * @apiSuccess {String} errmsg 前端提示消息
     * @apiSuccess {Object} data 结果集
     * @apiSuccessExample Success-Response:
     * HTTP/1.1 200 OK
    {
     *     "errno": 0,
     *     "errmsg": "成功"
     * }
     */
    @GetMapping("/hot")
    public Object hot(@NotNull Integer siteId,@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer size) {
        Page<BbsForum> pageVo = new Page<>();
        pageVo.setCurrent(page);
        pageVo.setSize(size);
        IPage<BbsForum> pList = bbsForumService.page(pageVo,
                new QueryWrapper<BbsForum>()
                        .eq("deleted",0)
                        .eq("site_id",siteId)
                        .eq("hot_tag",1)
                        .orderByDesc("add_time"));

        Map<String, Object> data = new HashMap<>();
        data.put("total", pList.getTotal());
        data.put("items", pList.getRecords());
        return ResponseUtil.ok(data);
    }

}

